package com.fanqwz.kotlinmvvm.presenter.viewmodel.login

import android.arch.lifecycle.MutableLiveData
import android.view.View
import com.fanqwz.kotlinmvvm.domain.`object`.User
import com.fanqwz.kotlinmvvm.domain.repo.UserRepository
import com.fanqwz.kotlinmvvm.domain.usecase.LoginParam
import com.fanqwz.kotlinmvvm.domain.usecase.LoginUseCase
import com.fanqwz.kotlinmvvm.domain.usecase.UseCaseSubject
import com.fanqwz.kotlinmvvm.presenter.viewmodel.BaseModel
import com.fanqwz.kotlinmvvm.presenter.viewmodel.BaseViewModel
import com.fanqwz.kotlinmvvm.presenter.viewmodel.IViewModel
import java.util.*
import javax.inject.Inject

/**
 * Created by fanqwz on 2018/3/2.
 */

class LoginModel @Inject constructor() : BaseModel<UserRepository>()

interface ILoginViewModel : IViewModel<LoginModel> {
    fun login(name: String, passwd: String)
}

class LoginViewModel @Inject constructor(val model: LoginModel, private val useCase: UseCaseSubject<User, LoginParam, LoginUseCase>) : BaseViewModel<LoginModel>(), ILoginViewModel, java.util.Observer {
    override fun update(o: Observable?, arg: Any?) {
        // do something
    }

    val name: MutableLiveData<String> = MutableLiveData()
    val passwd: MutableLiveData<String> = MutableLiveData()

    init {
        model.getObservable().addObserver(this)

        name.postValue("1")
        passwd.postValue("2")
    }

    override fun onCleared() {
        useCase.dispose()
        model.getObservable().deleteObserver(this)
        super.onCleared()
    }

    override fun login(name: String, passwd: String) {
        logger.logd("hehehe")
        useCase.execute(LoginParam(name, passwd)).subscribe {
            this.name.value = it.name
            this.passwd.value = it.password
        }
    }

    fun login(view: View) {
        login("pk004", "")
    }

    fun clicked(view: View) {
        login("pk004", "")
    }
}